"""
Author        yongfa
Date          2022-07-11 14:48:41
LastEditTime  2023-07-07 17:04:24
LastEditors   yongfa
Description   路由配置
"""

from flask_restx import Resource

from app.extensions.decorators import login_required, casbin_required
from app.extensions.utils.cache import user_cached
from .schemas import user_schemas, CommonSchema
from .parameters import params_dict
from .models import User
from .services import UserLoginServices, UserServices, UserInfoServices


def configure_router(ns):

    for _, schema in user_schemas.items():
        ns.add_model(schema.name, schema)

    @ns.route("")
    class UserListResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(parser=params_dict['get_list_params'], model=user_schemas['UserListSchema'])
        def get(self):
            """获取用户列表"""
            return UserServices(model=User, args=params_dict['get_list_params'], filter_delete=True).get_list()

        @ns.doc(body=params_dict['create_params'], model=user_schemas['UserDetailSchema'])
        def post(self):
            """创建用户详情"""
            return UserServices(model=User, args=params_dict['create_params'], filter_delete=True).create()

    @ns.route('/<int:id>')
    @ns.doc(params={"id": "The User ID"})
    class UserResource(Resource):
        method_decorators = [casbin_required]

        @ns.doc(model=user_schemas['UserDetailSchema'])
        def get(self, id):
            """获取用户详情"""
            return UserServices(model=User, args=None, filter_delete=True).get_detail(id)

        @ns.doc(body=params_dict['update_params'], model=user_schemas['UserDetailSchema'])
        def put(self, id):
            """修改用户详情"""
            return UserServices(model=User, args=params_dict['update_params'], filter_delete=True).update(id)

        @ns.doc(model=user_schemas['UserDetailSchema'])
        def delete(self, id):
            """删除用户详情"""
            return UserServices(model=User, args=None, filter_delete=True).soft_delete(id)

    @ns.route('/login')
    class UserLoginResource(Resource):
        @ns.doc(body=params_dict['login_params'], model=CommonSchema)
        def post(self):
            """用户登录"""
            return UserLoginServices(model=User, args=params_dict['login_params']).login()

    @ns.route('/refresh')
    class UserRefreshResource(Resource):
        @ns.doc(model=CommonSchema)
        def post(self):
            """刷新token"""
            return UserLoginServices(model=User).refresh()

    @ns.route('/logout')
    class UserLogoutResource(Resource):
        method_decorators = [login_required]

        @ns.doc(model=CommonSchema)
        def post(self):
            """用户登出"""
            return UserLoginServices(model=User).logout()

    @ns.route('/info')
    class UserInfoResource(Resource):
        method_decorators = [login_required]

        @ns.doc(body=params_dict['user_update_params'], model=user_schemas['UserDetailSchema'])
        def put(self):
            """修改用户自己信息"""
            return UserInfoServices(model=User, args=params_dict['user_update_params']).user_edit()

        @ns.doc(model=user_schemas['UserDetailSchema'])
        def get(self):
            """获取用户自己信息"""
            return UserInfoServices(model=User, args=None).user_info()

    @ns.route('/permissions')
    class UserPermissionsResource(Resource):
        method_decorators = [login_required]

        @ns.doc()
        @user_cached(timeout=10)
        def get(self):
            """获取用户自己权限"""
            return UserInfoServices(model=User, args=None).permissions()

    @ns.route('/password')
    class UserPasswordResource(Resource):
        method_decorators = [login_required]

        @ns.doc(body=params_dict['user_password_update_params'], model=CommonSchema)
        def post(self):
            """修改用户自己密码"""
            return UserInfoServices(model=User, args=params_dict['user_password_update_params']).user_password()
